import Time from "./Time.js";
function isRun(yea) {
  return yea % 400 ? yea % 100 ? !(yea % 4) : false : true;
}
function daysOfMonth(mon, yea) {
  // 根据年月判断当月日期数
  return {0: 31, 1: null, 2: 31, 3: 30, 4: 31, 5: 30, 6: 31, 7: 31, 8: 30, 9: 31, 10: 30, 11: 31}[mon] ||
    (isRun(yea) ? 29 : 28);
}
function getRenderYeaList(start, len, nextStart = []) {
  const shallShowWholeYear = nextStart[0] === undefined;
  const shallAddEmptyDat = nextStart[1] === undefined;
  const wholeMonlen = shallShowWholeYear ? undefined : (len + nextStart[0]);
  const yeaLen = shallShowWholeYear ? len : Math.ceil(wholeMonlen / 12);
  return new Array(yeaLen).fill(start).map((item, index) => {
    const restMonLen = shallShowWholeYear ? undefined : Math.ceil(wholeMonlen - index * 12);
    const thisMonLen = restMonLen > 12 ? 12 : restMonLen;
    const shallThisItemUseStart = !index && !shallShowWholeYear;
    const nextStartItem = shallThisItemUseStart ? nextStart.shift() : 0;
    const num = item + index;
    return {
      label: `yea`,
      num,
      list: getRenderMonList(
        nextStartItem,
        (thisMonLen || 12) - nextStartItem,
        nextStart,
        shallThisItemUseStart,
        num,
        shallAddEmptyDat,
      )
    }
  })
}
function getRenderMonList(start, len, nextStart = [], shallUseStart, yea, shallAddEmptyDat){
  return new Array(len).fill(start).map((item, index) => {
    const shallThisItemUseStart = !index && shallUseStart && nextStart.length;
    const nextStartItem = shallThisItemUseStart ? (nextStart.shift() || 1) : 1;
    const num = item + index;
    return {
      label: `mon`,
      num,
      list: new Array(shallAddEmptyDat ? ((new Date(yea, num, 1).getDay()) % 7) : 0).fill(undefined).concat(getRenderDayList(
        nextStartItem,
        daysOfMonth(num, yea) + 1 - nextStartItem,
      ))
    };
  });
}

function getRenderDayList(start, len) {
  return new Array(len).fill(start).map((item, index) => {
    const num = item + index;
    return {label: `dat`, num}
  })
}
/**
 param(yea, yeaLen)
 param(yea, monLen,?[startMon])
 param(yea, monLen,?[startMon,startDay])
 */
export default {
  getRenderList: getRenderYeaList,
};
